//
//  DHADViewController.m
//  Dresshere
//
//  Created by braspring on 2017/12/10.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "DHADViewController.h"
#import "DHADDetailViewController.h"
#import "DHLoginInfoViewController.h"
#import "DHLoginStyleViewController.h"

@interface DHADViewController (){
    dispatch_source_t _timer;
}

@property (weak, nonatomic) IBOutlet DHGradualButton *detailButton;
@property (weak, nonatomic) IBOutlet UIButton *skipButton;

@property (strong, nonatomic) NSArray *adArr;
@property (strong, nonatomic) NSDictionary *adDic;

@end

@implementation DHADViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self openCountdown];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self stopTimer];
}


- (NSArray *)adArr{
    if (!_adArr) {
        _adArr = [NSArray array];
    }
    return _adArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadADData];
    
    self.navView.hidden = YES;
    
    self.adImageView.userInteractionEnabled = YES;
    [self.adImageView bk_whenTapped:^{
        DHADDetailViewController *adDetailVC = [[DHADDetailViewController alloc] init];
        adDetailVC.url = [self.adDic objectForKey:@"url"];
        adDetailVC.navigationController.navigationBar.hidden = YES;
        [self.navigationController pushViewController:adDetailVC animated:YES];
    }];
    
    [self.skipButton bk_addEventHandler:^(id sender) {
        if ([Login isLogin]) {
            [kAppDelegate configHomeController];
        }else{
            [kAppDelegate configLoginController];
        }
        [self stopTimer];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)loadADData{
    [[DHNetAPIClient shareDHNetAPIClient] request_app_adWithParams:@{} AndBlock:^(id data, NSError *error) {
        if (data) {
            self.adArr = [data objectForKey:@"ads"];
        
            if (self.adArr.count > 0) {
                int index = arc4random() % self.adArr.count;
                self.adDic = [self.adArr objectAtIndex:index];
                [self.adImageView dh_setImageURLString:[self.adDic objectForKey:@"img"]];
            }else{
                [self.adImageView dh_setImageURLString:@""];
            }
        }
    }];
}

-(void) stopTimer{
    if(_timer){
        dispatch_source_cancel(_timer);
        _timer = nil;
    }
}

// 按钮倒计时效果
-(void)openCountdown{
    
    __weak typeof(self) weakSelf = self;
    
    __block NSInteger time = 4; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"%@",weakSelf.navigationController.viewControllers);
                if (weakSelf.navigationController.viewControllers.count > 1) {
                    
                }else{
                    if ([Login isLogin]) {
                        //                    if ([Login curLoginUser].height == 0 || [Login curLoginUser].weight == 0 || [Login curLoginUser].age == 0) { // 身高、体重、年龄未设置
                        //                        DHLoginInfoViewController *infoController = [[DHLoginInfoViewController alloc] init];
                        //                        [self.navigationController pushViewController:infoController animated:YES];
                        //                    }
                        //                    else if ([Login curLoginUser].styles.count != 0){ // 风格未设置
                        //                        DHLoginStyleViewController *styleController = [[DHLoginStyleViewController alloc] init];
                        //                        [self.navigationController pushViewController:styleController animated:YES];
                        //                    }
                        //                    else{
                        [kAppDelegate configHomeController];
                        //                    }
                    }else{
                        [kAppDelegate configLoginController];
                    }
                }
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.skipButton setTitle:[NSString stringWithFormat:@"%ld秒跳过",(long)time] forState:UIControlStateNormal];
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
